Skip to content

fix(tracing): catch ValueError and RecursionError in _safe_json_serialize#5419

Open
nileshpatil6 wants to merge 2 commits intogoogle:mainfrom
nileshpatil6:fix/safe-json-serialize-valueerror
Open

fix(tracing): catch ValueError and RecursionError in _safe_json_serialize#5419
nileshpatil6 wants to merge 2 commits intogoogle:mainfrom
nileshpatil6:fix/safe-json-serialize-valueerror

Conversation

@nileshpatil6
Copy link
Copy Markdown

Problem

_safe_json_serialize in telemetry/tracing.py only catches TypeError and OverflowError, but json.dumps can also raise:

  • ValueError — e.g. circular references (json.dumps raises ValueError: Circular reference detected)
  • RecursionError — e.g. deeply nested structures exceeding Python's recursion limit

Because this function is called inside finally blocks during tool execution telemetry, an uncaught exception here can interrupt normal execution flow and affect tool results.

Fixes #5411

Fix

Add ValueError and RecursionError to the existing except clause:

except (TypeError, OverflowError, ValueError, RecursionError):
    return '<not serializable>'

@nileshpatil6
Copy link
Copy Markdown
Author

I have read the Contributor License Agreement and I hereby agree to its terms.

@adk-bot adk-bot added the tracing [Component] This issue is related to OpenTelemetry tracing label Apr 20, 2026
@adk-bot
Copy link
Copy Markdown
Collaborator

adk-bot commented Apr 20, 2026

Response from ADK Triaging Agent

Hello @nileshpatil6, thank you for creating this PR!

Could you please include a testing plan section in your PR to describe how you tested your changes? This will help reviewers to review your PR more efficiently. Thanks!

@nileshpatil6
Copy link
Copy Markdown
Author

Testing Plan

Manual verification:

import json
from google.adk.telemetry.tracing import _safe_json_serialize

# Test 1: Circular reference (previously raised ValueError)
circular = {}
circular['self'] = circular
assert _safe_json_serialize(circular) == '<not serializable>'

# Test 2: Deeply nested structure (previously raised RecursionError)
deep = {}
node = deep
for _ in range(1000):
    node['child'] = {}
    node = node['child']
result = _safe_json_serialize(deep)
assert result == '<not serializable>'

# Test 3: Normal objects still serialize correctly
assert _safe_json_serialize({'key': 'value'}) == '{"key": "value"}'

print('All tests passed')

Before fix: _safe_json_serialize with a circular reference raises ValueError: Circular reference detected, which propagates out of the finally block and crashes tool execution.

After fix: Returns '<not serializable>' gracefully for all failure modes.

@rohityan rohityan self-assigned this Apr 20, 2026
@rohityan
Copy link
Copy Markdown
Collaborator

Hi @nileshpatil6 , Thank you for your contribution through this pull request! This PR has merge conflicts that require changes from your end. Could you please rebase your branch with the latest main branch to address these? Once this is complete, please let us know so we can proceed with the review.

@rohityan rohityan added the request clarification [Status] The maintainer need clarification or more information from the author label Apr 27, 2026
Upstream already added ValueError; add RecursionError to guard against
deeply nested structures exceeding Python's recursion limit when
json.dumps is called inside telemetry finally blocks.

Fixes google#5411
@nileshpatil6
Copy link
Copy Markdown
Author

Rebased on latest main. I noticed upstream already included ValueError in the except clause -- our change now only adds RecursionError to guard against deeply nested structures causing recursion during serialization. Please proceed with review when you get a chance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

request clarification [Status] The maintainer need clarification or more information from the author tracing [Component] This issue is related to OpenTelemetry tracing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

_safe_json_serialize in telemetry/tracing.py is not fully safe and can break tool execution when JSON serialization fails

3 participants